package shapes.shapes2D 
{
	import flash.display.CapsStyle;
	import flash.display.Graphics;
	import flash.geom.Point;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Arc extends BaseShape
	{
		
		private var _center:Point;
		private var _radius:Number;
		private var _startAngle:Number;
		private var _endAngle:Number;
		private var _steps:Number;
		
		private var _points:Vector.<Point>;
		
		public function Arc( center:Point, radius:Number, startAngle:Number, endAngle:Number, steps:Number = 60 ) 
		{
			
			this.center = center;
			this.radius = radius;
			this.startAngle = startAngle;
			this.endAngle = endAngle;
			this.steps = steps;
			
			resetShape();
			
		}
		
		private function resetShape():void 
		{
			
			var angleStep:Number = ( endAngle - startAngle ) / steps;
			
			points = new Vector.<Point>();
			
			for ( var i:int = 0; i < steps; i++ )
			{
				var angle:Number = ( startAngle + i * angleStep ) * Math.PI / 180;
				
				points.push( new Point( Math.cos( angle ) * radius, 
										Math.sin( angle ) * radius 	)	);
				
			}
			
		}
		
		public function draw( graphics:Graphics = null ):void
		{
			var ox:Number = center.x;
			var oy:Number = center.y;
			if ( graphics == null ) 
			{
				
				graphics = this.graphics;
				graphics.clear();
				graphics.lineStyle( strokeWidth, strokeColor, strokeAlpha, false, 'normal', CapsStyle.SQUARE );
				if( fillAlpha != 0 ) graphics.beginFill( fillColor, fillAlpha );
				ox = oy = 0;
			
			}
			
			var p:Point = points[ 0 ];
			graphics.moveTo( ox + p.x, oy + p.y );
			for each( p in points )
			{
				graphics.lineTo( ox + p.x, oy + p.y );
			}
			
		}
		
		public function get center():Point { return _center; }

		public function set center(value:Point):void 
		{
			_center = value;
			x = center.x;
			y = center.y;
		}
		
		public function get radius():Number { return _radius; }
		
		public function set radius(value:Number):void 
		{
			_radius = value;			
			resetShape();
		}
		
		public function get startAngle():Number { return _startAngle; }
		
		public function set startAngle(value:Number):void 
		{
			_startAngle = value;			
			resetShape();
		}
		
		public function get endAngle():Number { return _endAngle; }
		
		public function set endAngle(value:Number):void 
		{
			_endAngle = value;			
			resetShape();
		}
		
		public function get steps():Number { return _steps; }
		
		public function set steps(value:Number):void 
		{
			_steps = value;			
			resetShape();
		}
		
		public function get points():Vector.<Point> { return _points; }
		
		public function set points(value:Vector.<Point>):void 
		{
			_points = value;
		}
	}

}